package com.goldsign.pay.provider.user.service.impl;

import com.goldsign.pay.provider.user.service.MakeOrderNo;
import com.goldsign.pay.provider.user.to.AccountRegisterRespDTO;
import com.goldsign.pay.provider.user.vo.AccountRegOrderDO;
import com.goldsign.pay.common.exception.GcServiceException;
import com.goldsign.pay.common.service.impl.BaseServiceImpl;
import com.goldsign.pay.common.util.Constant;
import com.goldsign.pay.provider.user.service.AccountRegisterService;
import com.goldsign.pay.provider.user.service.UserService;
import com.goldsign.pay.provider.user.to.AccountRegisterReqDTO;
import com.goldsign.pay.provider.user.to.UserServiceResponseDTO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountRegisterServiceImpl extends BaseServiceImpl<AccountRegisterReqDTO> implements AccountRegisterService {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private UserService service;


	@Override
	public AccountRegisterRespDTO accountRegisterForAppStepOne(AccountRegisterReqDTO req) {

		logger.info("[收到 app账户注册申请(accountRegisterForAppStepOne) 请求]" + req);

		AccountRegisterRespDTO resp = new AccountRegisterRespDTO();

		try {
			if(StringUtils.isEmpty(req.getMob_no())||StringUtils.isEmpty(req.getTerminal_no())
					||StringUtils.isEmpty(req.getCustomer_ip())){
				resp.setResp_code(Constant.PARAMETER_ERROR);
				resp.setResp_msg("PARAMETER_ERROR");
				logger.info("参数不能为空：mob_no=" + req.getMob_no() + "terminal_no=" + req.getTerminal_no()
				+ "customer_ip=" + req.getCustomer_ip());
				return resp;
			}

			String orderNo = MakeOrderNo.makeOrderNo();
			String smsOrderNo = MakeOrderNo.makeOrderNo();

			//1 create register order
			service.insertAccountRegOrder(req, orderNo);

			//2 check if mob is registered
			boolean mobIsRegisterd = service.checkMobIsRegistered(req);
			if(mobIsRegisterd) {
				resp.setResp_code(Constant.MOB_IS_REGISTERED);
				resp.setResp_msg("MOB_IS_REGISTERED");

				return resp;
			}

			//3 record sms order
			service.insertSmsOrder(req, orderNo, smsOrderNo);

			//4 call sms send service
			service.sendRegisterMessage();

			resp.setOrder_no(orderNo);
			resp.setResp_code(Constant.SUCCESS);
			resp.setResp_msg("SUCCESS");

		} catch (GcServiceException e) {
			logger.error(e.getMessage());

			resp.setResp_code(e.getErrorCode());
			resp.setResp_msg(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());

			resp.setResp_code(Constant.SYSTERM_ERROR);
			resp.setResp_msg(e.getMessage());
		}

		logger.info("[返回 app账户注册申请(accountRegisterForAppStepOne) 响应]" + resp);
		return resp;
	}


	@Override
	public AccountRegisterRespDTO accountRegisterForAppStepTwo(AccountRegisterReqDTO req) {

		logger.info("[收到 app账户注册申请(accountRegisterForAppStepTwo) 请求]" + req);


		AccountRegisterRespDTO resp = new AccountRegisterRespDTO();

		try {
			if (StringUtils.isEmpty(req.getOrder_no())|| StringUtils.isEmpty(req.getTerminal_no())
					|| StringUtils.isEmpty(req.getVerify_code())) {
				resp.setResp_code(Constant.PARAMETER_ERROR);
				resp.setResp_msg("PARAMETER_ERROR");
				logger.info("参数不能为空：order_no=" + req.getOrder_no() + "terminal_no=" + req.getTerminal_no()
							+ "verify_code=" + req.getVerify_code());
				return resp;
			}
			// 查询注册申请订单
			AccountRegOrderDO order = service.getAccountRegOrderByOrderNo(req.getOrder_no());
			if(order == null) {
				resp.setResp_code(Constant.REGISTER_ORDER_IS_NOT_EXISTS);
				resp.setResp_msg("REGISTER_ORDER_IS_NOT_EXISTS");
				return resp;
			}

			// 校验短信验证码
			UserServiceResponseDTO userServiceResponseDTO = service.validateSmsVerifyCode(order.getOrder_no(),
					req.getTerminal_no(), req.getVerify_code(), "01");
			if(!Constant.SUCCESS.equals(userServiceResponseDTO.getResp_code())) {
				resp.setResp_code(userServiceResponseDTO.getResp_code());
				resp.setResp_msg(userServiceResponseDTO.getResp_msg());
				return resp;
			}

			// 返回
			resp.setOrder_no(req.getOrder_no());
			resp.setResp_code(Constant.SUCCESS);
			resp.setResp_msg("SUCCESS");

		}catch (GcServiceException e) {
			logger.error(e.getMessage());

			resp.setResp_code(e.getErrorCode());
			resp.setResp_msg(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());

			resp.setResp_code(Constant.SYSTERM_ERROR);
			resp.setResp_msg("SYSTERM_ERROR");
		}

		logger.info("[返回 app账户注册申请(accountRegisterForAppStepTwo) 响应]" + resp);
		return resp;
	}


	@Override
	public AccountRegisterRespDTO accountRegisterForAppStepThree(AccountRegisterReqDTO req) {

		logger.info("[收到 app账户注册申请(accountRegisterForAppStepThree) 请求]" + req);

		String orderNo = req.getOrder_no();
		AccountRegisterRespDTO resp = new AccountRegisterRespDTO();

		try {
			if (StringUtils.isEmpty(orderNo) || StringUtils.isEmpty(req.getLogin_passwd())) {
				resp.setResp_code(Constant.PARAMETER_ERROR);
				resp.setResp_msg("PARAMETER_ERROR");
				logger.info("参数不能为空：order_no=" + orderNo + "login_passwd=" + req.getLogin_passwd());
				return resp;
			}

			service.updateAccountRegOrder(req);

			// 查询注册申请订单
			AccountRegOrderDO order = service.getAccountRegOrderByOrderNo(orderNo);

			// 创建用户信息
			String userNo = service.createUserAccount(order);

			// 异步更新订单状态

			// 返回
			resp.setUser_no(userNo);
			resp.setResp_code(Constant.SUCCESS);
			resp.setResp_msg("SUCCESS");

		}catch (GcServiceException e) {
			logger.error(e.getMessage());

			resp.setResp_code(e.getErrorCode());
			resp.setResp_msg(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());

			resp.setResp_code(Constant.SYSTERM_ERROR);
			resp.setResp_msg("SYSTERM_ERROR");
		}

		logger.info("[返回 app账户注册申请(accountRegisterForAppStepThree) 响应]" + resp);
		return resp;
	}
}
